'use strict';
angular.module('app')
    .controller('forumCommentReplyController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
    	
            $scope.title = '评论回复管理';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param = {};
            
            $scope.search = function () {
                $scope.loading = true;
                
                $.ajax({
                    url : $scope.app.project_name + '/forumCommentReply/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name + '/forumCommentReply/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            $scope.clearSearch = function() {
                $scope.searchParams = { };
                $scope.search();
            }
            
            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum = pageNo;
                $scope.search();
            };

            $scope.enable = function(id, enable) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要启用或禁用的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (enable == 'Y') {
            		msg = '启用';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.enable == 'N' && item.checked) {
                            count++;
                        } else if (item.checked && item.enable != 'N') {
                            nocount++;
                        }
                    })
            	}
            	
            	if (enable == 'N') {
            		msg = '停用';
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的评论或回复吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/forumCommentReply/update/enable',
        				data: {'ids': idStr, 'enable':enable},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					//$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction) {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide = function() {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview1 = function(introduction) {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.left = event.clientX;
                showDiv1.style.top = event.clientY;
                showDiv1.style.display = 'block';
                showDiv1.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide1 = function() {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.display = 'none';
                showDiv1.innerHTML = '';
            }
            
            $scope.stopShow = function () {
                $scope.myShow = false;
            };
            
            //预览
            $scope.onPreview = function (data) {
                console.log(data)
                $scope.myShow = true;
                $scope.dataUrl = data.messageVideo;
            };
            
            $scope.getFileExt=function (str) {
                var d=/\.[^\.]+$/.exec(str);
                return d[0].toLowerCase();
            }

        } ]);